package srx.open.com.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import srx.open.com.R;
import srx.open.com.bean.CoursePublish;
import srx.open.com.utils.DataBaseHelper;
import srx.open.com.utils.DateUtils;

/**
 * 首页推荐的adpter.
 */
public class HomeRecommendAdpter extends BaseCoursePublishAdpter {

    public HomeRecommendAdpter(Context context, List<CoursePublish> pubList, DataBaseHelper dataBaseHelper) {
        super(context, pubList, dataBaseHelper);
    }

    @Override
    public View bindView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder = null;
        View row = convertView;
        if (convertView == null) {
            row = mInflater.inflate(R.layout.listitem_home_recommend_info, parent,
                    false);
            holder = new MyViewHolder(row);
            row.setTag(holder);
        } else {
            holder = (MyViewHolder) row.getTag();
        }

        try {
            // 获取课程信息.
            CoursePublish coursePublish = mPubList.get(position);
            String title = coursePublish.getTitle(); // 课程标题.
            String content = coursePublish.getContent(); // 课程内容.
//        String createDate = coursePublish.getUpdatedAt(); // 创建日期.
            String createDate = coursePublish.getCreatedAt(); // 创建日期.
            if (TextUtils.isEmpty(title)) {
                title = "无标题";
            }
            if (TextUtils.isEmpty(content)) {
                content = "无内容";
            }
            if (title.length() > MAX_TITLE_NUMBER)
                title = title.substring(0, MAX_TITLE_NUMBER) + "...";
            if (content.length() > MAX_CONTENT_NUMBER)
                content = content.substring(0, MAX_CONTENT_NUMBER) + "...";
            String tempTime = DateUtils.daysBetween(mContext, createDate);
            // 地区设置
//        int provinceId = coursePublish.getProvinceId();
            int cityId = coursePublish.getCityId();
            int districtId = coursePublish.getDistrictId();
//        String provinceName = mDataBaseHelper.queryIdToAddrName(DataBaseHelper.DATA_PROVINCE, provinceId);
            String cityName = mDataBaseHelper.queryIdToAddrName(DataBaseHelper.DATA_CITY, cityId);
            String districtName = mDataBaseHelper.queryIdToAddrName(DataBaseHelper.DATA_DISTRICT, districtId);
            if (cityName.contains("行政单位") || cityName.contains("自治州"))
                cityName = "";
            holder.home_recommend_location_tv.setText("来自于" + cityName + districtName);
            //
            holder.home_recommend_time_tv.setText("" + tempTime);
            holder.home_recommend_title_tv.setText("" + title); // 课程标题栏.
            holder.home_recommend_content_tv.setText("" + content); // 课程内容
        } catch (Exception e) {
            e.printStackTrace();
        }
        return row;
    }

}
